package com.wbs.shop.nuby.view.base;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wbs.shop.nuby.view.listener.OnClickAdapterListenter;
import com.wbs.shop.nuby.view.listener.RecoveryListener;

import java.util.List;


/**
 * Created by Administrator on 2017/1/16 0016.
 */

public abstract class BaseAdapter<V, B> extends android.widget.BaseAdapter implements RecoveryListener{

    protected List<B> datas;
    protected Context mContext;
    protected LayoutInflater inflater;
    protected View.OnClickListener mOnClickListener;
    protected OnClickAdapterListenter mOnClickAdapterListenter;

    public BaseAdapter(Context mContext) {
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<B> datas) {
        this.datas = datas;
    }

    public List<B> getDatas() {
        return datas;
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public B getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取适配器
     *
     * @param view
     * @return
     */
    public abstract V getViewHolder(View view);

    /**
     * 获取布局id
     *
     * @return
     */
    public abstract int getItemLoayoutId();

    /**
     * 数据处理
     *
     * @param holder
     * @param bean
     */
    public abstract void viewHolderHandle(V holder, B bean, int position);

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        V holder = null;
        if (convertView == null) {// 如果是第一次显示该页面(要记得保存到viewholder中供下次直接从缓存中调用)
            convertView = inflater.inflate(getItemLoayoutId(), null);
            holder = getViewHolder(convertView);
            convertView.setTag(holder);
        } else {// 如果之前已经显示过该页面，则用viewholder中的缓存直接刷屏
            holder = (V) convertView.getTag();
        }
        viewHolderHandle(holder, getItem(position), position);
        return convertView;
    }

    public void setOnClickListener(View.OnClickListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    public void setOnClickAdapterListenter(OnClickAdapterListenter mOnClickAdapterListenter) {
        this.mOnClickAdapterListenter = mOnClickAdapterListenter;
    }

    @Override
    public void onDestroy() {

    }
}
